<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"><title>Chapter 14: Overloaded Operations And Conversions | A君的秘密基地</title><meta name="keywords" content="C++ Primer"><meta name="author" content="astralcon"><meta name="copyright" content="astralcon"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="# Exercises Section 14.1 Ex14.1不同点：重载操作符必须至少有一个类类型或枚举类型的操作数；重载操作符不保证操作数的求值顺序。相同点：有相同的优先级和结合性，操作数的数目不变。 Ex14.212345678910class Sales_data&amp;#123;	friend ostream &amp;operator&lt;&lt;(ostream &amp;, cons">
<meta property="og:type" content="article">
<meta property="og:title" content="Chapter 14: Overloaded Operations And Conversions">
<meta property="og:url" content="https://astralcon.gitee.io/blog/2022/05/14/Chapter-14-Overloaded-Operations-And-Conversions/index.html">
<meta property="og:site_name" content="A君的秘密基地">
<meta property="og:description" content="# Exercises Section 14.1 Ex14.1不同点：重载操作符必须至少有一个类类型或枚举类型的操作数；重载操作符不保证操作数的求值顺序。相同点：有相同的优先级和结合性，操作数的数目不变。 Ex14.212345678910class Sales_data&amp;#123;	friend ostream &amp;operator&lt;&lt;(ostream &amp;, cons">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="">
<meta property="article:published_time" content="2022-05-14T06:28:26.000Z">
<meta property="article:modified_time" content="2022-05-14T06:59:18.856Z">
<meta property="article:author" content="astralcon">
<meta property="article:tag" content="C++ Primer">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content=""><link rel="shortcut icon" href="/blog/img/avatar.jpg"><link rel="canonical" href="https://astralcon.gitee.io/blog/2022/05/14/Chapter-14-Overloaded-Operations-And-Conversions/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/blog/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@6/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/blog/',
  algolia: {"appId":"YUB0ZFWBBL","apiKey":"78d0f1fae31a1df3e0cb573725c55558","indexName":"blog","hits":{"per_page":6},"languages":{"input_placeholder":"搜索文章","hits_empty":"找不到您查询的内容：${query}","hits_stats":"找到 ${hits} 条结果，用时 ${time} 毫秒"}},
  localSearch: undefined,
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '天',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery@2/dist/fjGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery@2/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'Chapter 14: Overloaded Operations And Conversions',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2022-05-14 14:59:18'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><meta name="generator" content="Hexo 6.2.0"></head><body><div id="loading-box"><div class="loading-left-bg"></div><div class="loading-right-bg"></div><div class="spinner-box"><div class="configure-border-1"><div class="configure-core"></div></div><div class="configure-border-2"><div class="configure-core"></div></div><div class="loading-word">加载中...</div></div></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="/blog/img/avatar.jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/blog/archives/"><div class="headline">文章</div><div class="length-num">27</div></a><a href="/blog/tags/"><div class="headline">标签</div><div class="length-num">4</div></a><a href="/blog/categories/"><div class="headline">分类</div><div class="length-num">3</div></a></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/blog/"><i class="fa-fw fas fa-home"></i><span> 首頁</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fa fa-folder-open"></i><span> 目录</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/blog/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></li><li><a class="site-page child" href="/blog/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></li><li><a class="site-page child" href="/blog/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></li></ul></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fa fa-heartbeat"></i><span> 清单</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/blog/game/"><i class="fa-fw fas fa-gamepad"></i><span> 游戏</span></a></li><li><a class="site-page child" href="/blog/book/"><i class="fa-fw fas fa-book"></i><span> 书籍</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/blog/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/blog/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/blog/">A君的秘密基地</a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/blog/"><i class="fa-fw fas fa-home"></i><span> 首頁</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fa fa-folder-open"></i><span> 目录</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/blog/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></li><li><a class="site-page child" href="/blog/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></li><li><a class="site-page child" href="/blog/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></li></ul></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fa fa-heartbeat"></i><span> 清单</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/blog/game/"><i class="fa-fw fas fa-gamepad"></i><span> 游戏</span></a></li><li><a class="site-page child" href="/blog/book/"><i class="fa-fw fas fa-book"></i><span> 书籍</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/blog/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/blog/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">Chapter 14: Overloaded Operations And Conversions</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2022-05-14T06:28:26.000Z" title="发表于 2022-05-14 14:28:26">2022-05-14</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2022-05-14T06:59:18.856Z" title="更新于 2022-05-14 14:59:18">2022-05-14</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/blog/categories/C/">C++</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-commentcount"><i class="far fa-comments fa-fw post-meta-icon"></i><span class="post-meta-label">评论数:</span><a href="/blog/2022/05/14/Chapter-14-Overloaded-Operations-And-Conversions/#post-comment" itemprop="discussionUrl"><span class="valine-comment-count" data-xid="/blog/2022/05/14/Chapter-14-Overloaded-Operations-And-Conversions/" itemprop="commentCount"></span></a></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><p>﻿# Exercises Section 14.1</p>
<h2 id="Ex14-1"><a href="#Ex14-1" class="headerlink" title="Ex14.1"></a>Ex14.1</h2><p>不同点：重载操作符必须至少有一个类类型或枚举类型的操作数；重载操作符不保证操作数的求值顺序。<br>相同点：有相同的优先级和结合性，操作数的数目不变。</p>
<h2 id="Ex14-2"><a href="#Ex14-2" class="headerlink" title="Ex14.2"></a>Ex14.2</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">Sales_data</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="keyword">friend</span> ostream &amp;<span class="keyword">operator</span>&lt;&lt;(ostream &amp;, <span class="type">const</span> Sales_data &amp;);</span><br><span class="line">	<span class="keyword">friend</span> istream &amp;<span class="keyword">operator</span>&gt;&gt;(istream &amp;, Sales_data &amp;);</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">	Sales_data &amp;<span class="keyword">operator</span>+=(<span class="type">const</span> Sales_data &amp;);</span><br><span class="line">	<span class="comment">// 其他公有成员、私有成员和友元函数不变</span></span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">Sales_data <span class="keyword">operator</span>+(<span class="type">const</span> Sales_data &amp;, <span class="type">const</span> Sales_data &amp;);</span><br></pre></td></tr></table></figure>

<h2 id="Ex14-3"><a href="#Ex14-3" class="headerlink" title="Ex14.3"></a>Ex14.3</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">vector&lt;string&gt; svec1, svec2;</span><br><span class="line">a) <span class="string">&quot;cobble&quot;</span> == <span class="string">&quot;stone&quot;</span>		<span class="comment">// 内置类型 ==</span></span><br><span class="line">b) svec1[<span class="number">0</span>] == svec2[<span class="number">0</span>]		<span class="comment">// 使用 string 的 ==</span></span><br><span class="line">c) svec1 == svec2			<span class="comment">// 使用 vector 的 == </span></span><br><span class="line">d) svec1[<span class="number">0</span>] == <span class="string">&quot;stone&quot;</span>		<span class="comment">// 使用 string 的 ==</span></span><br></pre></td></tr></table></figure>

<h2 id="Ex14-4"><a href="#Ex14-4" class="headerlink" title="Ex14.4"></a>Ex14.4</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">a) %	<span class="comment">// 定义为非成员</span></span><br><span class="line">b) %=	<span class="comment">// 最好被定义为成员</span></span><br><span class="line">c) ++	<span class="comment">// 定义为成员</span></span><br><span class="line">d) -&gt;	<span class="comment">// 定义为成员</span></span><br><span class="line">e) &lt;&lt;	<span class="comment">// 定义为非成员</span></span><br><span class="line">f) &amp;&amp;	<span class="comment">// 定义为非成员</span></span><br><span class="line">g) ==	<span class="comment">// 定义为非成员</span></span><br><span class="line">h) ()	<span class="comment">//  定义为成员</span></span><br></pre></td></tr></table></figure>

<h2 id="Ex14-5"><a href="#Ex14-5" class="headerlink" title="Ex14.5"></a>Ex14.5</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">Book</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="keyword">friend</span> std::ostream &amp;<span class="keyword">operator</span>&lt;&lt;(std::ostream &amp;, <span class="type">const</span> Book &amp;);</span><br><span class="line">	<span class="keyword">friend</span> std::istream &amp;<span class="keyword">operator</span>&gt;&gt;(std::istream &amp;, Book &amp;);</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="built_in">Book</span>() = <span class="keyword">default</span>;</span><br><span class="line">    <span class="built_in">Book</span>(<span class="type">const</span> std::string s) : <span class="built_in">bookNo</span>(s) &#123;&#125;</span><br><span class="line">    <span class="built_in">Book</span>(<span class="type">const</span> std::string s, <span class="type">const</span> std::String n, <span class="type">const</span> <span class="type">double</span> p) : <span class="built_in">BookNo</span>(s), <span class="built_in">author_name</span>(n), <span class="built_in">revenue</span>(p) &#123;&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">private</span>:</span><br><span class="line">    std::string bookNo;</span><br><span class="line">    std::string author_name;</span><br><span class="line">    <span class="type">double</span> price;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">std::ostream &amp;<span class="keyword">operator</span>&lt;&lt;(std::ostream &amp;os, <span class="type">const</span> Book &amp;b)</span><br><span class="line">&#123;</span><br><span class="line">	os &lt;&lt; b.bookNo &lt;&lt; <span class="string">&quot; &quot;</span> &lt;&lt; b.author_name &lt;&lt; <span class="string">&quot; &quot;</span> &lt;&lt; b.price;</span><br><span class="line">	<span class="keyword">return</span> os;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">std::istream &amp;<span class="keyword">operator</span>&gt;&gt;(std::istream &amp;is, Book &amp;b)</span><br><span class="line">&#123;</span><br><span class="line">	is &gt;&gt; b.bookNo &gt;&gt; b.author_name &gt;&gt; b.price;</span><br><span class="line">	<span class="keyword">if</span> (!is)</span><br><span class="line">		b = <span class="built_in">Book</span>();</span><br><span class="line">	<span class="keyword">return</span> is;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h1 id="Exercises-Section-14-2-1"><a href="#Exercises-Section-14-2-1" class="headerlink" title="Exercises Section 14.2.1"></a>Exercises Section 14.2.1</h1><h2 id="Ex14-6"><a href="#Ex14-6" class="headerlink" title="Ex14.6"></a>Ex14.6</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">ostream &amp;<span class="keyword">operator</span>&lt;&lt;(ostream &amp;os, <span class="type">const</span> Sales_data &amp;item)</span><br><span class="line">&#123;</span><br><span class="line">	os &lt;&lt; item.<span class="built_in">isbn</span>() &lt;&lt; <span class="string">&quot; &quot;</span> &lt;&lt; item.units_sold &lt;&lt; <span class="string">&quot; &quot;</span> &lt;&lt; item.revenue &lt;&lt; <span class="string">&quot; &quot;</span> &lt;&lt; item.<span class="built_in">avg_price</span>();</span><br><span class="line">	<span class="keyword">return</span> os;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="Ex14-7"><a href="#Ex14-7" class="headerlink" title="Ex14.7"></a>Ex14.7</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">ostream &amp;<span class="keyword">operator</span>&lt;&lt;(osteam &amp;os, <span class="type">const</span> string &amp;s)</span><br><span class="line">&#123;</span><br><span class="line">	<span class="keyword">return</span> <span class="built_in">print</span>(os, s);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="Ex14-8"><a href="#Ex14-8" class="headerlink" title="Ex14.8"></a>Ex14.8</h2><p>见 <font color=red>Ex14.5</font></p>
<h1 id="Exercises-Section-14-2-2"><a href="#Exercises-Section-14-2-2" class="headerlink" title="Exercises Section 14.2.2"></a>Exercises Section 14.2.2</h1><h2 id="Ex14-9"><a href="#Ex14-9" class="headerlink" title="Ex14.9"></a>Ex14.9</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">isteam &amp;<span class="keyword">operator</span>&gt;&gt;(istream &amp;is, Sales_data &amp;item)</span><br><span class="line">&#123;</span><br><span class="line">	<span class="type">double</span> price;</span><br><span class="line">	is &gt;&gt; item.bookNo &gt;&gt; item.units_sold &gt;&gt; price;</span><br><span class="line">	<span class="keyword">if</span> (is)</span><br><span class="line">		item.revenue = item.units_sold * price;</span><br><span class="line">	<span class="keyword">else</span></span><br><span class="line">		item = <span class="built_in">Sales_data</span>();</span><br><span class="line">	<span class="keyword">return</span> is;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="Ex14-10"><a href="#Ex14-10" class="headerlink" title="Ex14.10"></a>Ex14.10</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">a) <span class="number">0</span><span class="number">-201</span><span class="number">-99999</span>-<span class="number">-9</span> <span class="number">10</span> <span class="number">24.95</span>	<span class="comment">// 正常输入</span></span><br><span class="line">b) <span class="number">10</span> <span class="number">24.95</span> <span class="number">0</span><span class="number">-210</span><span class="number">-99999</span><span class="number">-9</span>	<span class="comment">// 输入类型对应错误，最后会调用默认构造函数</span></span><br></pre></td></tr></table></figure>

<h2 id="Ex14-11"><a href="#Ex14-11" class="headerlink" title="Ex14.11"></a>Ex14.11</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 如果输入的类型对应错误，那么就无法处理错误情况</span></span><br><span class="line"><span class="comment">// 上个练习中 a 情况正常，b 就会发生错误</span></span><br><span class="line">isteam &amp;<span class="keyword">operator</span>&gt;&gt;(istream &amp;in, Sales_data &amp;s)</span><br><span class="line">&#123;</span><br><span class="line">	<span class="type">double</span> price;</span><br><span class="line">	in &gt;&gt; s.bookNo &gt;&gt; s.units_sold &gt;&gt; price;</span><br><span class="line">	s.revenue = s.units_sold * price;</span><br><span class="line">	<span class="keyword">return</span> in;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="Ex14-12"><a href="#Ex14-12" class="headerlink" title="Ex14.12"></a>Ex14.12</h2><p>见 <font color=red>Ex14.5</font></p>
<h1 id="Exercises-Section-14-3"><a href="#Exercises-Section-14-3" class="headerlink" title="Exercises Section 14.3"></a>Exercises Section 14.3</h1><h2 id="Ex14-13"><a href="#Ex14-13" class="headerlink" title="Ex14.13"></a>Ex14.13</h2><p>还可以定义减法运算符</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">Sales_data &amp;Sales_data::<span class="keyword">operator</span>-=(<span class="type">const</span> Sales_data &amp;rhs)</span><br><span class="line">&#123;</span><br><span class="line">	units_sold -= rhs.units_sold;</span><br><span class="line">	revenue -= rhs.revenue;</span><br><span class="line">	<span class="keyword">return</span> *<span class="keyword">this</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">Sales_data <span class="keyword">operator</span>-(<span class="type">const</span> Sales_data &amp;lhs, <span class="type">const</span> Sales_data &amp;rhs)</span><br><span class="line">&#123;</span><br><span class="line">	Sales_data sub = lhs;</span><br><span class="line">	sub -= rhs;</span><br><span class="line">	<span class="keyword">return</span> sub;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="Ex14-14"><a href="#Ex14-14" class="headerlink" title="Ex14.14"></a>Ex14.14</h2><p>代码复用</p>
<h2 id="Ex14-15"><a href="#Ex14-15" class="headerlink" title="Ex14.15"></a>Ex14.15</h2><p>定义的 Book 类不需要算术运算符</p>
<h1 id="Exercises-Section-14-3-1"><a href="#Exercises-Section-14-3-1" class="headerlink" title="Exercises Section 14.3.1"></a>Exercises Section 14.3.1</h1><h2 id="Ex14-16"><a href="#Ex14-16" class="headerlink" title="Ex14.16"></a>Ex14.16</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">StrBolb</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="keyword">friend</span> <span class="type">bool</span> <span class="keyword">operator</span>==(<span class="type">const</span> StrBolb &amp;, <span class="type">const</span> StrBolb &amp;);</span><br><span class="line">	<span class="keyword">friend</span> <span class="type">bool</span> <span class="keyword">operator</span>!=(<span class="type">const</span> StrBolb &amp;, <span class="type">const</span> StrBolb &amp;);</span><br><span class="line">	<span class="comment">// 其他公有成员、私有成员和友元函数不变</span></span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="type">bool</span> <span class="keyword">operator</span>==(<span class="type">const</span> StrBolb &amp;lhs, <span class="type">const</span> StrBolb &amp;rhs)</span><br><span class="line">&#123;</span><br><span class="line">	<span class="keyword">return</span> lhs.data == rhs.data;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">bool</span> <span class="keyword">operator</span>!=(<span class="type">const</span> StrBolb &amp;lhs, <span class="type">const</span> StrBolb &amp;rhs)</span><br><span class="line">&#123;</span><br><span class="line">	<span class="keyword">return</span> !(lhs == rhs);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">StrBlobPtr</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="keyword">friend</span> <span class="type">bool</span> <span class="keyword">operator</span>==(<span class="type">const</span> StrBlobPtr &amp;, <span class="type">const</span> StrBlobPtr &amp;);</span><br><span class="line">	<span class="keyword">friend</span> <span class="type">bool</span> <span class="keyword">operator</span>!=(<span class="type">const</span> StrBlobPtr &amp;, <span class="type">const</span> StrBlobPtr &amp;);</span><br><span class="line">	<span class="comment">// 其他公有成员、私有成员和友元函数不变</span></span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="type">bool</span> <span class="keyword">operator</span>==(<span class="type">const</span> StrBlobPtr &amp;lhs, <span class="type">const</span> StrBlobPtr &amp;rhs)</span><br><span class="line">&#123;</span><br><span class="line">	<span class="keyword">return</span> lhs.curr = rhs.curr &amp;&amp;</span><br><span class="line">		   lhs.wptr.<span class="built_in">lock</span>() == rhs.wptr.<span class="built_in">lock</span>();</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">bool</span> <span class="keyword">operator</span>!=(<span class="type">const</span> StrBlobPtr &amp;lhs, <span class="type">const</span> StrBlobPtr &amp;rhs)</span><br><span class="line">&#123;</span><br><span class="line">	<span class="keyword">return</span> !(lhs == rhs);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">StrVec</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="keyword">friend</span> <span class="type">bool</span> <span class="keyword">operator</span>==(<span class="type">const</span> StrVec &amp;, <span class="type">const</span> StrVec &amp;);</span><br><span class="line">	<span class="keyword">friend</span> <span class="type">bool</span> <span class="keyword">operator</span>!=(<span class="type">const</span> StrVec &amp;, <span class="type">const</span> StrVec &amp;);</span><br><span class="line">	<span class="comment">// 其他公有成员、私有成员和友元函数不变</span></span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="type">bool</span> <span class="keyword">operator</span>==(<span class="type">const</span> StrVec &amp;lhs, <span class="type">const</span> StrVec &amp;rhs)</span><br><span class="line">&#123;</span><br><span class="line">	<span class="keyword">if</span> (lhs.<span class="built_in">size</span>() != rhs.<span class="built_in">size</span>())</span><br><span class="line">		<span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">	<span class="keyword">for</span> (<span class="keyword">auto</span> it1 = lhs.<span class="built_in">begin</span>(). it2 = rhs.<span class="built_in">begin</span>(); it1 != lhs.<span class="built_in">end</span>() &amp;&amp; it2 != rhs.<span class="built_in">end</span>(); ++it1, ++it2)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="keyword">if</span> (*it1 != *it2)</span><br><span class="line">			<span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">bool</span> <span class="keyword">operator</span>!=(<span class="type">const</span> StrVec &amp;lhs, <span class="type">const</span> StrVec &amp;rhs)</span><br><span class="line">&#123;</span><br><span class="line">	<span class="keyword">return</span> !(lhs == rhs);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="Ex14-17"><a href="#Ex14-17" class="headerlink" title="Ex14.17"></a>Ex14.17</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">Book</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="keyword">friend</span> <span class="type">bool</span> <span class="keyword">operator</span>==(<span class="type">const</span> Book &amp;, <span class="type">const</span> Book &amp;);</span><br><span class="line">	<span class="keyword">friend</span> <span class="type">bool</span> <span class="keyword">operator</span>!=(<span class="type">const</span> Book &amp;, <span class="type">const</span> Book &amp;);</span><br><span class="line">	<span class="comment">// 其他公有成员、私有成员和友元函数不变</span></span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="type">bool</span> <span class="keyword">operator</span>==(<span class="type">const</span> Book &amp;lhs, <span class="type">const</span> Book &amp;rhs)</span><br><span class="line">&#123;</span><br><span class="line">	<span class="keyword">return</span> lhs.bookNo == rhs.bookNo &amp;&amp;</span><br><span class="line">		   lhs.author_name == rhs.author_name &amp;&amp;</span><br><span class="line">		   lhs.price == rhs.price;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">bool</span> <span class="keyword">operator</span>!=(<span class="type">const</span> Book &amp;lhs, <span class="type">const</span> Book &amp;rhs)</span><br><span class="line">&#123;</span><br><span class="line">	<span class="keyword">return</span> !(lhs == rhs);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h1 id="Exercises-Section-14-3-2"><a href="#Exercises-Section-14-3-2" class="headerlink" title="Exercises Section 14.3.2"></a>Exercises Section 14.3.2</h1><h2 id="Ex14-18"><a href="#Ex14-18" class="headerlink" title="Ex14.18"></a>Ex14.18</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">StrBolb</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="keyword">friend</span> <span class="type">bool</span> <span class="keyword">operator</span>&lt;(<span class="type">const</span> StrBolb &amp;, <span class="type">const</span> StrBolb &amp;);</span><br><span class="line">	<span class="keyword">friend</span> <span class="type">bool</span> <span class="built_in">operator</span>&lt;=(<span class="type">const</span> StrBolb &amp;, <span class="type">const</span> StrBolb &amp;);</span><br><span class="line">	<span class="keyword">friend</span> <span class="type">bool</span> <span class="keyword">operator</span>&gt;(<span class="type">const</span> StrBolb &amp;, <span class="type">const</span> StrBolb &amp;);</span><br><span class="line">	<span class="keyword">friend</span> <span class="type">bool</span> <span class="keyword">operator</span>&gt;=(<span class="type">const</span> StrBolb &amp;, <span class="type">const</span> StrBolb &amp;);</span><br><span class="line">	<span class="comment">// 其他公有成员、私有成员和友元函数不变</span></span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="type">bool</span> <span class="keyword">operator</span>&lt;(<span class="type">const</span> StrBolb &amp;lhs, <span class="type">const</span> StrBolb &amp;rhs)</span><br><span class="line">&#123;</span><br><span class="line">	<span class="keyword">return</span> *lhs.data &lt; *rhs.data;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">bool</span> <span class="keyword">operator</span>&lt;=(<span class="type">const</span> StrBolb &amp;lhs, <span class="type">const</span> StrBolb &amp;rhs)</span><br><span class="line">&#123;</span><br><span class="line">	<span class="keyword">return</span> *lhs.data &lt;= *rhs.data;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">bool</span> <span class="keyword">operator</span>&gt;(<span class="type">const</span> StrBolb &amp;lhs, <span class="type">const</span> StrBolb &amp;rhs)</span><br><span class="line">&#123;</span><br><span class="line">	<span class="keyword">return</span> *lhs.data &gt; *rhs.data;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">bool</span> <span class="keyword">operator</span>&gt;=(<span class="type">const</span> StrBolb &amp;lhs, <span class="type">const</span> StrBolb &amp;rhs)</span><br><span class="line">&#123;</span><br><span class="line">	<span class="keyword">return</span> *lhs.data &gt;= *rhs.data;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">StrBlobPtr</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="keyword">friend</span> <span class="type">bool</span> <span class="keyword">operator</span>&lt;(<span class="type">const</span> StrBlobPtr &amp;, <span class="type">const</span> StrBlobPtr &amp;);</span><br><span class="line">	<span class="keyword">friend</span> <span class="type">bool</span> <span class="built_in">operator</span>&lt;=(<span class="type">const</span> StrBlobPtr &amp;, <span class="type">const</span> StrBlobPtr &amp;);</span><br><span class="line">	<span class="keyword">friend</span> <span class="type">bool</span> <span class="keyword">operator</span>&gt;(<span class="type">const</span> StrBlobPtr &amp;, <span class="type">const</span> StrBlobPtr &amp;);</span><br><span class="line">	<span class="keyword">friend</span> <span class="type">bool</span> <span class="keyword">operator</span>&gt;=(<span class="type">const</span> StrBlobPtr &amp;, <span class="type">const</span> StrBlobPtr &amp;);</span><br><span class="line">	<span class="comment">// 其他公有成员、私有成员和友元函数不变</span></span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="type">bool</span> <span class="keyword">operator</span>&lt;(<span class="type">const</span> StrBlobPtr &amp;lhs, <span class="type">const</span> StrBlobPtr &amp;rhs)</span><br><span class="line">&#123;</span><br><span class="line">	<span class="keyword">auto</span> l = lhs.wptr.<span class="built_in">lock</span>(), r = rhs.wptr.<span class="built_in">lock</span>();</span><br><span class="line">	<span class="keyword">if</span> (l == r)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="keyword">if</span> (!r)</span><br><span class="line">			<span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">		<span class="keyword">return</span> lhs.curr &lt; rhs.curr;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">else</span> </span><br><span class="line">		<span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">bool</span> <span class="keyword">operator</span>&lt;=(<span class="type">const</span> StrBlobPtr &amp;lhs, <span class="type">const</span> StrBlobPtr &amp;rhs)</span><br><span class="line">&#123;</span><br><span class="line">	<span class="keyword">auto</span> l = lhs.wptr.<span class="built_in">lock</span>(), r = rhs.wptr.<span class="built_in">lock</span>();</span><br><span class="line">	<span class="keyword">if</span> (l == r)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="keyword">if</span> (!r)</span><br><span class="line">			<span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">		<span class="keyword">return</span> lhs.curr &lt;= rhs.curr;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">else</span> </span><br><span class="line">		<span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">bool</span> <span class="keyword">operator</span>&gt;(<span class="type">const</span> StrBlobPtr &amp;lhs, <span class="type">const</span> StrBlobPtr &amp;rhs)</span><br><span class="line">&#123;</span><br><span class="line">	<span class="keyword">auto</span> l = lhs.wptr.<span class="built_in">lock</span>(), r = rhs.wptr.<span class="built_in">lock</span>();</span><br><span class="line">	<span class="keyword">if</span> (l == r)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="keyword">if</span> (!r)</span><br><span class="line">			<span class="keyword">return</span> <span class="literal">false</span>;`在这里插入代码片`</span><br><span class="line">		<span class="keyword">return</span> lhs.curr &gt; rhs.curr;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">else</span> </span><br><span class="line">		<span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">bool</span> <span class="keyword">operator</span>&gt;=(<span class="type">const</span> StrBlobPtr &amp;lhs, <span class="type">const</span> StrBlobPtr &amp;rhs)</span><br><span class="line">&#123;</span><br><span class="line">	<span class="keyword">auto</span> l = lhs.wptr.<span class="built_in">lock</span>(), r = rhs.wptr.<span class="built_in">lock</span>();</span><br><span class="line">	<span class="keyword">if</span> (l == r)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="keyword">if</span> (!r)</span><br><span class="line">			<span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">		<span class="keyword">return</span> lhs.curr &gt;= rhs.curr;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">else</span> </span><br><span class="line">		<span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">StrVec</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="keyword">friend</span> <span class="type">bool</span> <span class="keyword">operator</span>&lt;(<span class="type">const</span> StrVec &amp;, <span class="type">const</span> StrVec &amp;);</span><br><span class="line">	<span class="keyword">friend</span> <span class="type">bool</span> <span class="built_in">operator</span>&lt;=(<span class="type">const</span> StrVec &amp;, <span class="type">const</span> StrVec &amp;);</span><br><span class="line">	<span class="keyword">friend</span> <span class="type">bool</span> <span class="keyword">operator</span>&gt;(<span class="type">const</span> StrVec &amp;, <span class="type">const</span> StrVec &amp;);</span><br><span class="line">	<span class="keyword">friend</span> <span class="type">bool</span> <span class="keyword">operator</span>&gt;=(<span class="type">const</span> StrVec &amp;, <span class="type">const</span> StrVec &amp;);</span><br><span class="line">	<span class="comment">// 其他公有成员、私有成员和友元函数不变</span></span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="type">bool</span> <span class="keyword">operator</span>&lt;(<span class="type">const</span> StrVec &amp;lhs, <span class="type">const</span> StrVec &amp;rhs)</span><br><span class="line">&#123;</span><br><span class="line">	<span class="keyword">for</span> (<span class="keyword">auto</span> it1 = lhs.<span class="built_in">begin</span>(), it2 = rhs.<span class="built_in">begin</span>(); it1 != lhs.<span class="built_in">end</span>(), it2 != rhs.<span class="built_in">end</span>(); ++it1, ++it2)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="keyword">if</span> (*it1 &lt; *it2)</span><br><span class="line">			<span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">		<span class="keyword">if</span> (*it1 &gt; *it2)</span><br><span class="line">			<span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">if</span> (lhs.<span class="built_in">size</span>() &lt; rhs.<span class="built_in">size</span>())</span><br><span class="line">		<span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">	<span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">bool</span> <span class="keyword">operator</span>&lt;=(<span class="type">const</span> StrVec &amp;lhs, <span class="type">const</span> StrVec &amp;rhs)</span><br><span class="line">&#123;</span><br><span class="line">	<span class="keyword">for</span> (<span class="keyword">auto</span> it1 = lhs.<span class="built_in">begin</span>(), it2 = rhs.<span class="built_in">begin</span>(); it1 != lhs.<span class="built_in">end</span>(), it2 != rhs.<span class="built_in">end</span>(); ++it1, ++it2)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="keyword">if</span> (*it1 &lt;= *it2)</span><br><span class="line">			<span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">if</span> (lhs.<span class="built_in">size</span>() &lt;= rhs.<span class="built_in">size</span>())</span><br><span class="line">		<span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">	<span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">bool</span> <span class="keyword">operator</span>&gt;(<span class="type">const</span> StrVec &amp;lhs, <span class="type">const</span> StrVec &amp;rhs)</span><br><span class="line">&#123;</span><br><span class="line">	<span class="keyword">for</span> (<span class="keyword">auto</span> it1 = lhs.<span class="built_in">begin</span>(), it2 = rhs.<span class="built_in">begin</span>(); it1 != lhs.<span class="built_in">end</span>(), it2 != rhs.<span class="built_in">end</span>(); ++it1, ++it2)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="keyword">if</span> (*it1 &gt; *it2)</span><br><span class="line">			<span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">		<span class="keyword">if</span> (*it1 &lt; *it2)</span><br><span class="line">			<span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">if</span> (lhs.<span class="built_in">size</span>() &gt; rhs.<span class="built_in">size</span>())</span><br><span class="line">		<span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">	<span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">bool</span> <span class="keyword">operator</span>&gt;=(<span class="type">const</span> StrVec &amp;lhs, <span class="type">const</span> StrVec &amp;rhs)</span><br><span class="line">&#123;</span><br><span class="line">	<span class="keyword">for</span> (<span class="keyword">auto</span> it1 = lhs.<span class="built_in">begin</span>(), it2 = rhs.<span class="built_in">begin</span>(); it1 != lhs.<span class="built_in">end</span>(), it2 != rhs.<span class="built_in">end</span>(); ++it1, ++it2)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="keyword">if</span> (*it1 &gt;= *it2)</span><br><span class="line">			<span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">if</span> (lhs.<span class="built_in">size</span>() &gt;= rhs.<span class="built_in">size</span>())</span><br><span class="line">		<span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">	<span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="Ex14-19"><a href="#Ex14-19" class="headerlink" title="Ex14.19"></a>Ex14.19</h2><p>不需要，书本进行比较没有多大意义。</p>
<h1 id="Exercises-Section-14-4"><a href="#Exercises-Section-14-4" class="headerlink" title="Exercises Section 14.4"></a>Exercises Section 14.4</h1><h2 id="Ex14-20"><a href="#Ex14-20" class="headerlink" title="Ex14.20"></a>Ex14.20</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">Sales_data &amp;Sales_data::<span class="keyword">operator</span>+=(<span class="type">const</span> Sales_data &amp;rhs)</span><br><span class="line">&#123;</span><br><span class="line">	units_sold += rhs.units_sold;</span><br><span class="line">	revenue += rhs.revenue;</span><br><span class="line">	<span class="keyword">return</span> *<span class="keyword">this</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">Sales_data <span class="keyword">operator</span>+(<span class="type">const</span> Sales_data &amp;lhs, <span class="type">const</span> Sales_data &amp;rhs)</span><br><span class="line">&#123;</span><br><span class="line">	Sales_data sum = lhs;</span><br><span class="line">	sum += rhs;</span><br><span class="line">	<span class="keyword">return</span> sum;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="Ex14-21"><a href="#Ex14-21" class="headerlink" title="Ex14.21"></a>Ex14.21</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">Sales_data &amp;Sales_data::<span class="keyword">operator</span>+=(<span class="type">const</span> Sales_data &amp;rhs)</span><br><span class="line">&#123;</span><br><span class="line">	*<span class="keyword">this</span> = (*<span class="keyword">this</span>) + rhs;</span><br><span class="line">	<span class="keyword">return</span> *<span class="keyword">this</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">Sales_data <span class="keyword">operator</span>+(<span class="type">const</span> Sales_data &amp;lhs, <span class="type">const</span> Sales_data &amp;rhs)</span><br><span class="line">&#123;</span><br><span class="line">	Sales_data sum = lhs;</span><br><span class="line">	sum.units_sold += rhs.units_sold;</span><br><span class="line">	sum.revenue += rhs.revenue;</span><br><span class="line">	<span class="keyword">return</span> sum;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>operator+ 需要被定义为友元，可读性差。</p>
<h2 id="Ex14-22"><a href="#Ex14-22" class="headerlink" title="Ex14.22"></a>Ex14.22</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">Sales_data &amp;Sales_data::<span class="keyword">operator</span>=(<span class="type">const</span> string &amp;s)</span><br><span class="line">&#123;</span><br><span class="line">	bookNo = s;</span><br><span class="line">	<span class="keyword">return</span> *<span class="keyword">this</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="Ex14-23"><a href="#Ex14-23" class="headerlink" title="Ex14.23"></a>Ex14.23</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">StrVec &amp;StrVec::<span class="keyword">operator</span>=(initializer_list&lt;string&gt; il)</span><br><span class="line">&#123;</span><br><span class="line">	<span class="keyword">auto</span> newdata = <span class="built_in">alloc_n_copy</span>(il.<span class="built_in">begin</span>(), il.<span class="built_in">end</span>());</span><br><span class="line">	<span class="built_in">free</span>();</span><br><span class="line">	elements = newdata.first;</span><br><span class="line">	first_free = newdata.second;</span><br><span class="line">	cap = newdata.second;</span><br><span class="line">	<span class="keyword">return</span> *<span class="keyword">this</span>;	</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="Ex14-24"><a href="#Ex14-24" class="headerlink" title="Ex14.24"></a>Ex14.24</h2><p>使用合成的拷贝赋值运算符即可</p>
<h2 id="Ex14-25"><a href="#Ex14-25" class="headerlink" title="Ex14.25"></a>Ex14.25</h2><p>定义一个赋值运算符，把 string 赋值给 Book</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">Book &amp;Book::<span class="keyword">operator</span>=(<span class="type">const</span> string &amp;s)</span><br><span class="line">&#123;</span><br><span class="line">	istringstream <span class="built_in">in</span>(s);</span><br><span class="line">	in &gt;&gt; bookNo &gt;&gt; author_name &gt;&gt; price;</span><br><span class="line">	<span class="keyword">if</span> (!in)</span><br><span class="line">		*<span class="keyword">this</span> = <span class="built_in">Book</span>();</span><br><span class="line">	<span class="keyword">return</span> *<span class="keyword">this</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h1 id="Exercises-Section-14-5"><a href="#Exercises-Section-14-5" class="headerlink" title="Exercises Section 14.5"></a>Exercises Section 14.5</h1><h2 id="Ex14-26"><a href="#Ex14-26" class="headerlink" title="Ex14.26"></a>Ex14.26</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">StrBlob</span></span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">	std::string &amp; <span class="keyword">operator</span>[](std::<span class="type">size_t</span> n) &#123; <span class="built_in">check</span>(n, <span class="string">&quot;out of range&quot;</span>); <span class="keyword">return</span> (*data)[n]; &#125;</span><br><span class="line">	<span class="type">const</span> std::string &amp; <span class="keyword">operator</span>[](std::<span class="type">size_t</span> n) <span class="type">const</span> &#123; <span class="built_in">check</span>(n, <span class="string">&quot;out of range&quot;</span>); <span class="keyword">return</span> (*data)[n]; &#125;</span><br><span class="line">	<span class="comment">// 其他公有成员、私有成员和友元函数不变</span></span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">StrBlobPtr</span></span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">	std::string &amp; <span class="keyword">operator</span>[](std::<span class="type">size_t</span> n) &#123; <span class="keyword">auto</span> p = <span class="built_in">check</span>(n, <span class="string">&quot;out of range&quot;</span>); <span class="keyword">return</span> (*p)[n]; &#125;</span><br><span class="line">	<span class="type">const</span> std::string &amp; <span class="keyword">operator</span>[](std::<span class="type">size_t</span> n) <span class="type">const</span> &#123; <span class="keyword">auto</span> p = <span class="built_in">check</span>(n, <span class="string">&quot;out of range&quot;</span>); <span class="keyword">return</span> (*p)[n]; &#125;</span><br><span class="line">	<span class="comment">// 其他公有成员、私有成员和友元函数不变</span></span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">StrVec</span></span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">	std::string &amp; <span class="keyword">operator</span>[](std::<span class="type">size_t</span> n) &#123; <span class="keyword">return</span> elements[n]; &#125;</span><br><span class="line">	<span class="type">const</span> std::string &amp; <span class="keyword">operator</span>[](std::<span class="type">size_t</span> n) <span class="type">const</span>; &#123; <span class="keyword">return</span> elements[n]; &#125;</span><br><span class="line">	<span class="comment">// 其他公有成员、私有成员和友元函数不变</span></span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<h1 id="Exercises-Section-14-6"><a href="#Exercises-Section-14-6" class="headerlink" title="Exercises Section 14.6"></a>Exercises Section 14.6</h1><h2 id="Ex14-27"><a href="#Ex14-27" class="headerlink" title="Ex14.27"></a>Ex14.27</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">StrBlobPtr</span></span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">	StrBlobPtr &amp; <span class="keyword">operator</span>++();</span><br><span class="line">	StrBlobPtr &amp; <span class="keyword">operator</span>--();</span><br><span class="line">	StrBlobPtr <span class="keyword">operator</span>++(<span class="type">int</span>);</span><br><span class="line">	StrBlobPtr <span class="keyword">operator</span>--(<span class="type">int</span>);</span><br><span class="line">	<span class="comment">// 其他公有成员、私有成员和友元函数不变</span></span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">StrBlobPtr &amp;StrBlobPtr::<span class="keyword">operator</span>++()</span><br><span class="line">&#123;</span><br><span class="line">	<span class="built_in">check</span>(curr, <span class="string">&quot;increment past end of StrBlobPtr&quot;</span>);</span><br><span class="line">	++curr;</span><br><span class="line">	<span class="keyword">return</span> *<span class="keyword">this</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">StrBlobPtr &amp;StrBlobPtr::<span class="keyword">operator</span>--()</span><br><span class="line">&#123;</span><br><span class="line">	--curr;</span><br><span class="line">	<span class="built_in">check</span>(curr, <span class="string">&quot;decrement past begin of StrBlobPtr&quot;</span>);</span><br><span class="line">	reurn *<span class="keyword">this</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">StrBlobPtr StrBlobPtr::<span class="keyword">operator</span>++(<span class="type">int</span>)</span><br><span class="line">&#123;</span><br><span class="line">	StrBlobPtr ret = *<span class="keyword">this</span>;</span><br><span class="line">	++*<span class="keyword">this</span>;</span><br><span class="line">	<span class="keyword">return</span> ret;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">StrBlobPtr StrBlobPtr::<span class="keyword">operator</span>--(<span class="type">int</span>)</span><br><span class="line">&#123;</span><br><span class="line">	StrBlobPtr ret = *<span class="keyword">this</span>;</span><br><span class="line">	--*<span class="keyword">this</span>;</span><br><span class="line">	<span class="keyword">return</span> ret;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="Ex14-28"><a href="#Ex14-28" class="headerlink" title="Ex14.28"></a>Ex14.28</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">StrBlobPtr</span></span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">	StrBlobPtr &amp; <span class="keyword">operator</span>+=(<span class="type">const</span> <span class="type">size_t</span> &amp;);</span><br><span class="line">	StrBlobPtr &amp; <span class="keyword">operator</span>-=(<span class="type">const</span> <span class="type">size_t</span> &amp;);</span><br><span class="line">	<span class="comment">// 其他公有成员、私有成员和友元函数不变</span></span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">StrBlobPtr &amp;StrBlobPtr::<span class="keyword">operator</span>+=(<span class="type">const</span> <span class="type">size_t</span> &amp;n)</span><br><span class="line">&#123;</span><br><span class="line">	<span class="built_in">check</span>(curr, <span class="string">&quot;increment past end of StrBlobPtr&quot;</span>);</span><br><span class="line">	curr += n;</span><br><span class="line">	<span class="keyword">return</span> *<span class="keyword">this</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">StrBlobPtr &amp;StrBlobPtr::<span class="keyword">operator</span>-=(<span class="type">const</span> <span class="type">size_t</span> &amp;n)</span><br><span class="line">&#123;</span><br><span class="line">	curr -= n;</span><br><span class="line">	<span class="built_in">check</span>(curr, <span class="string">&quot;decrement past begin of StrBlobPtr&quot;</span>);</span><br><span class="line">	<span class="keyword">return</span> *<span class="keyword">this</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="Ex14-29"><a href="#Ex14-29" class="headerlink" title="Ex14.29"></a>Ex14.29</h2><p>递增和递减都会改变对象的值，故而不能能定义为const。</p>
<h1 id="Exercises-Section-14-7"><a href="#Exercises-Section-14-7" class="headerlink" title="Exercises Section 14.7"></a>Exercises Section 14.7</h1><h2 id="Ex14-30"><a href="#Ex14-30" class="headerlink" title="Ex14.30"></a>Ex14.30</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">StrBlobPtr</span></span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">	std::string &amp; <span class="keyword">operator</span>*() <span class="type">const</span> </span><br><span class="line">	&#123; </span><br><span class="line">		<span class="keyword">auto</span> p = <span class="built_in">check</span>(curr, <span class="string">&quot;dereference past end&quot;</span>);</span><br><span class="line">	  	<span class="keyword">return</span> (*p)[curr]; </span><br><span class="line">	&#125;</span><br><span class="line">	std::string * <span class="keyword">operator</span>-&gt;() <span class="type">const</span></span><br><span class="line">	&#123;	</span><br><span class="line">		<span class="keyword">return</span> &amp; <span class="keyword">this</span>-&gt;<span class="keyword">operator</span>*();</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="comment">// 其他公有成员、私有成员和友元函数不变</span></span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">ConstStrBlobPtr</span></span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">	<span class="type">const</span> std::string &amp; <span class="keyword">operator</span>*() <span class="type">const</span> </span><br><span class="line">	&#123; </span><br><span class="line">		<span class="keyword">auto</span> p = <span class="built_in">check</span>(curr, <span class="string">&quot;dereference past end&quot;</span>);</span><br><span class="line">	  	<span class="keyword">return</span> (*p)[curr]; </span><br><span class="line">	&#125;</span><br><span class="line">	<span class="type">const</span> std::string * <span class="keyword">operator</span>-&gt;() <span class="type">const</span></span><br><span class="line">	&#123;	</span><br><span class="line">		<span class="keyword">return</span> &amp; <span class="keyword">this</span>-&gt;<span class="keyword">operator</span>*();</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="comment">// 其他公有成员、私有成员和友元函数不变</span></span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<h2 id="Ex14-31"><a href="#Ex14-31" class="headerlink" title="Ex14.31"></a>Ex14.31</h2><p>因为 StrBlobPtr 类对于拷贝构造、赋值操作和析构都不需要执行另外的内容，故而使用编译器合成的即可。</p>
<h2 id="Ex14-32"><a href="#Ex14-32" class="headerlink" title="Ex14.32"></a>Ex14.32</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">PointStr</span></span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">	std::string * <span class="keyword">operator</span>-&gt;()</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="keyword">return</span> p-&gt;<span class="keyword">operator</span>-&gt;();</span><br><span class="line">	&#125;</span><br><span class="line"><span class="keyword">private</span>:</span><br><span class="line">	StrBlobPtr *p;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<h1 id="Exercises-Section-14-8"><a href="#Exercises-Section-14-8" class="headerlink" title="Exercises Section 14.8"></a>Exercises Section 14.8</h1><h2 id="Ex14-33"><a href="#Ex14-33" class="headerlink" title="Ex14.33"></a>Ex14.33</h2><p>0个或多个</p>
<h2 id="Ex14-34"><a href="#Ex14-34" class="headerlink" title="Ex14.34"></a>Ex14.34</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">ReturnPara</span></span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">	<span class="built_in">ReturnPara</span>() = <span class="keyword">default</span>;</span><br><span class="line">	<span class="built_in">ReturnPara</span>(<span class="type">int</span> i, <span class="type">int</span> j, <span class="type">int</span> k): <span class="built_in">a</span>(i), <span class="built_in">b</span>(j), <span class="built_in">c</span>(k) &#123; &#125;</span><br><span class="line">	<span class="function"><span class="type">int</span> <span class="title">operator</span><span class="params">()</span><span class="params">(<span class="type">const</span> <span class="type">int</span> &amp;i, <span class="type">const</span> <span class="type">int</span> &amp;j, <span class="type">const</span> <span class="type">int</span> &amp;k)</span>  <span class="type">const</span></span></span><br><span class="line"><span class="function">	</span>&#123;</span><br><span class="line">		<span class="keyword">if</span> (i)</span><br><span class="line">			<span class="keyword">return</span> j;</span><br><span class="line">		<span class="keyword">else</span> </span><br><span class="line">			<span class="keyword">return</span> k;</span><br><span class="line">	&#125;</span><br><span class="line"><span class="keyword">private</span>:</span><br><span class="line">	<span class="type">int</span> a;</span><br><span class="line">	<span class="type">int</span> b;</span><br><span class="line">	<span class="type">int</span> c;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="Ex14-35"><a href="#Ex14-35" class="headerlink" title="Ex14.35"></a>Ex14.35</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">ReadString</span></span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">	<span class="built_in">ReadString</span>(istream &amp;is = cin): <span class="built_in">in</span>(is) &#123; &#125;</span><br><span class="line">	<span class="function">string <span class="title">operator</span><span class="params">()</span><span class="params">()</span></span></span><br><span class="line"><span class="function">	</span>&#123;</span><br><span class="line">		string temp;</span><br><span class="line">		<span class="keyword">if</span> (!<span class="built_in">getline</span>(in, temp))</span><br><span class="line">			temp = <span class="string">&quot;&quot;</span>;</span><br><span class="line">		<span class="keyword">return</span> temp;</span><br><span class="line">	&#125;</span><br><span class="line"><span class="keyword">private</span>:</span><br><span class="line">	istream &amp;in;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<h2 id="Ex14-36"><a href="#Ex14-36" class="headerlink" title="Ex14.36"></a>Ex14.36</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;vector&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;string&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;algorithm&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">ReadString</span></span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">	<span class="built_in">ReadString</span>(istream &amp;is = cin): <span class="built_in">in</span>(is) &#123; &#125;</span><br><span class="line">	<span class="function">string <span class="title">operator</span><span class="params">()</span><span class="params">()</span></span></span><br><span class="line"><span class="function">	</span>&#123;</span><br><span class="line">		string temp;</span><br><span class="line">		<span class="keyword">if</span> (!<span class="built_in">getline</span>(in, temp))</span><br><span class="line">			temp = <span class="string">&quot;&quot;</span>;</span><br><span class="line">		<span class="keyword">return</span> temp;</span><br><span class="line">	&#125;</span><br><span class="line"><span class="keyword">private</span>:</span><br><span class="line">	istream &amp;in;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	vector&lt;string&gt; vec;</span><br><span class="line">	string line;</span><br><span class="line">	<span class="keyword">while</span> (<span class="literal">true</span>)</span><br><span class="line">	&#123;</span><br><span class="line">		string line = <span class="built_in">ReadString</span>()();</span><br><span class="line">		<span class="keyword">if</span> (!line.<span class="built_in">empty</span>())</span><br><span class="line">			vec.<span class="built_in">push_back</span>(line);</span><br><span class="line">		<span class="keyword">else</span> </span><br><span class="line">			<span class="keyword">break</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	for_each(vec.<span class="built_in">begin</span>(), vec.<span class="built_in">end</span>(), [](string &amp;s) &#123; cout &lt;&lt; s &lt;&lt; endl; &#125;);</span><br><span class="line"></span><br><span class="line">	<span class="built_in">system</span>(<span class="string">&quot;pause&quot;</span>);</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="Ex14-37"><a href="#Ex14-37" class="headerlink" title="Ex14.37"></a>Ex14.37</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">CompareString</span></span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">	<span class="built_in">CompareString</span>(string s = <span class="built_in">string</span>()): <span class="built_in">str</span>(s) &#123; &#125;</span><br><span class="line">	<span class="function"><span class="type">bool</span> <span class="title">operator</span><span class="params">()</span><span class="params">(string &amp;s)</span> <span class="type">const</span> </span>&#123; <span class="keyword">return</span> s == str; &#125;</span><br><span class="line"><span class="keyword">private</span>:</span><br><span class="line">	string str;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<h1 id="Exercises-Section-14-8-1"><a href="#Exercises-Section-14-8-1" class="headerlink" title="Exercises Section 14.8.1"></a>Exercises Section 14.8.1</h1><h2 id="Ex14-38"><a href="#Ex14-38" class="headerlink" title="Ex14.38"></a>Ex14.38</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;fstream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;sstream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;algorithm&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;vector&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;string&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">CompareString</span></span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">	<span class="built_in">CompareString</span>(<span class="type">size_t</span> n): <span class="built_in">sz</span>(n) &#123; &#125;</span><br><span class="line">	<span class="function"><span class="type">bool</span> <span class="title">operator</span><span class="params">()</span><span class="params">(<span class="type">const</span> string &amp;s)</span> <span class="type">const</span></span></span><br><span class="line"><span class="function">	</span>&#123;</span><br><span class="line">		<span class="keyword">return</span> s.<span class="built_in">size</span>() == sz;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">private</span>:</span><br><span class="line">	<span class="type">size_t</span> sz;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">char</span> *argv[])</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	vector&lt;string&gt; words;</span><br><span class="line">	string word;</span><br><span class="line">	<span class="function">ifstream <span class="title">in</span><span class="params">(argv[<span class="number">1</span>])</span></span>;</span><br><span class="line">	string line;</span><br><span class="line">	<span class="keyword">while</span> (<span class="built_in">getline</span>(in, line))</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="function">istringstream <span class="title">iss</span><span class="params">(line)</span></span>;</span><br><span class="line">		<span class="keyword">while</span> (iss &gt;&gt; word)</span><br><span class="line">		&#123;</span><br><span class="line">			<span class="keyword">if</span> (<span class="built_in">ispunct</span>(word[<span class="number">0</span>]))</span><br><span class="line">				word.<span class="built_in">erase</span>(<span class="number">0</span>, <span class="number">1</span>);</span><br><span class="line">			<span class="keyword">if</span> (<span class="built_in">ispunct</span>(word[word.<span class="built_in">size</span>() - <span class="number">1</span>]))</span><br><span class="line">				word.<span class="built_in">pop_back</span>();</span><br><span class="line">			words.<span class="built_in">push_back</span>(word);</span><br><span class="line">			cout &lt;&lt; word &lt;&lt; <span class="string">&quot; &quot;</span>;</span><br><span class="line">		&#125;</span><br><span class="line">		cout &lt;&lt; endl;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">for</span> (<span class="type">size_t</span> i = <span class="number">1</span>; i != <span class="number">11</span>; ++i)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="type">int</span> cnt = <span class="number">0</span>;</span><br><span class="line">		<span class="keyword">auto</span> wc = words.<span class="built_in">begin</span>();</span><br><span class="line">		<span class="keyword">while</span> (wc != words.<span class="built_in">end</span>())</span><br><span class="line">		&#123;</span><br><span class="line">			wc = <span class="built_in">find_if</span>(wc, words.<span class="built_in">end</span>(), <span class="built_in">CompareString</span>(i));</span><br><span class="line">			<span class="keyword">if</span> (wc != words.<span class="built_in">end</span>())</span><br><span class="line">			&#123;</span><br><span class="line">				cout &lt;&lt; *wc &lt;&lt; <span class="string">&quot; &quot;</span>;</span><br><span class="line">				++wc;</span><br><span class="line">				++cnt;</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">		cout &lt;&lt; endl;</span><br><span class="line">		cout &lt;&lt; <span class="string">&quot;length &quot;</span> &lt;&lt; i &lt;&lt; <span class="string">&quot; has &quot;</span> &lt;&lt; cnt &lt;&lt; endl;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="built_in">system</span>(<span class="string">&quot;pause&quot;</span>);</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="Ex14-39"><a href="#Ex14-39" class="headerlink" title="Ex14.39"></a>Ex14.39</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;fstream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;sstream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;algorithm&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;vector&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;string&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">BoundSize</span></span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">	<span class="built_in">BoundSize</span>(<span class="type">size_t</span> b, <span class="type">size_t</span> t): <span class="built_in">bottom</span>(b), <span class="built_in">top</span>(t) &#123; &#125;</span><br><span class="line">	<span class="function"><span class="type">bool</span> <span class="title">operator</span><span class="params">()</span><span class="params">(<span class="type">const</span> string &amp;s)</span> <span class="type">const</span></span></span><br><span class="line"><span class="function">	</span>&#123;</span><br><span class="line">		<span class="keyword">return</span> s.<span class="built_in">size</span>() &gt;= bottom &amp;&amp; s.<span class="built_in">size</span>() &lt;= top;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">private</span>:</span><br><span class="line">	<span class="type">size_t</span> bottom;</span><br><span class="line">	<span class="type">size_t</span> top;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">LongerSize</span></span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">	<span class="built_in">LongerSize</span>(<span class="type">size_t</span> s): <span class="built_in">sz</span>(s) &#123; &#125;</span><br><span class="line">	<span class="function"><span class="type">bool</span> <span class="title">operator</span><span class="params">()</span><span class="params">(<span class="type">const</span> string &amp;s)</span> <span class="type">const</span> </span>&#123; <span class="keyword">return</span> s.<span class="built_in">size</span>() &gt;= sz; &#125;</span><br><span class="line"><span class="keyword">private</span>:</span><br><span class="line">	<span class="type">size_t</span> sz;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">char</span> *argv[])</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	vector&lt;string&gt; words;</span><br><span class="line">	string word;</span><br><span class="line">	<span class="function">ifstream <span class="title">in</span><span class="params">(argv[<span class="number">1</span>])</span></span>;</span><br><span class="line">	string line;</span><br><span class="line">	<span class="keyword">while</span> (<span class="built_in">getline</span>(in, line))</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="function">istringstream <span class="title">iss</span><span class="params">(line)</span></span>;</span><br><span class="line">		<span class="keyword">while</span> (iss &gt;&gt; word)</span><br><span class="line">		&#123;</span><br><span class="line">			<span class="keyword">if</span> (<span class="built_in">ispunct</span>(word[<span class="number">0</span>]))</span><br><span class="line">				word.<span class="built_in">erase</span>(<span class="number">0</span>, <span class="number">1</span>);</span><br><span class="line">			<span class="keyword">if</span> (<span class="built_in">ispunct</span>(word[word.<span class="built_in">size</span>() - <span class="number">1</span>]))</span><br><span class="line">				word.<span class="built_in">pop_back</span>();</span><br><span class="line">			words.<span class="built_in">push_back</span>(word);</span><br><span class="line">			cout &lt;&lt; word &lt;&lt; <span class="string">&quot; &quot;</span>;</span><br><span class="line">		&#125;</span><br><span class="line">		cout &lt;&lt; endl;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">for</span> (<span class="type">size_t</span> i = <span class="number">1</span>; i != <span class="number">11</span>; ++i)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="type">int</span> cnt = <span class="number">0</span>;</span><br><span class="line">		<span class="keyword">auto</span> wc = words.<span class="built_in">begin</span>();</span><br><span class="line">		<span class="keyword">while</span> (wc != words.<span class="built_in">end</span>())</span><br><span class="line">		&#123;</span><br><span class="line">			wc = <span class="built_in">find_if</span>(wc, words.<span class="built_in">end</span>(), <span class="built_in">BoundSize</span>(<span class="number">1</span>, <span class="number">9</span>));</span><br><span class="line">			<span class="keyword">if</span> (wc != words.<span class="built_in">end</span>())</span><br><span class="line">			&#123;</span><br><span class="line">				cout &lt;&lt; *wc &lt;&lt; <span class="string">&quot; &quot;</span>;</span><br><span class="line">				++wc;</span><br><span class="line">				++cnt;</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">		cout &lt;&lt; endl;</span><br><span class="line">		cout &lt;&lt; <span class="string">&quot;length 1 - 9 has &quot;</span> &lt;&lt; cnt &lt;&lt; endl;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	cnt = <span class="number">0</span>;</span><br><span class="line">	wc = words.<span class="built_in">begin</span>();</span><br><span class="line">	<span class="keyword">while</span> (wc != words.<span class="built_in">end</span>())</span><br><span class="line">	&#123;</span><br><span class="line">		wc = <span class="built_in">find_if</span>(wc, words.<span class="built_in">end</span>(), <span class="built_in">LongerSize</span>(<span class="number">10</span>));</span><br><span class="line">		<span class="keyword">if</span> (wc != words.<span class="built_in">end</span>())</span><br><span class="line">		&#123;</span><br><span class="line">			cout &lt;&lt; *wc &lt;&lt; <span class="string">&quot; &quot;</span>;</span><br><span class="line">			++wc;</span><br><span class="line">			++cnt;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	cout &lt;&lt; endl;</span><br><span class="line">	cout &lt;&lt; <span class="string">&quot;Length longer than 10 has &quot;</span> &lt;&lt; cnt &lt;&lt; endl;</span><br><span class="line">	<span class="built_in">system</span>(<span class="string">&quot;pause&quot;</span>);</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="Ex14-40"><a href="#Ex14-40" class="headerlink" title="Ex14.40"></a>Ex14.40</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;algorithm&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;vector&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;fstream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;sstream&gt;</span></span></span><br><span class="line"> </span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"> </span><br><span class="line"><span class="keyword">class</span> <span class="title class_">IsShorter</span> </span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">	<span class="function"><span class="type">bool</span> <span class="title">operator</span><span class="params">()</span><span class="params">(<span class="type">const</span> string &amp;s1, <span class="type">const</span> string &amp;s2)</span> </span>&#123; <span class="keyword">return</span> s1.<span class="built_in">size</span>() &lt; s2.<span class="built_in">size</span>(); &#125;</span><br><span class="line">&#125;;</span><br><span class="line"> </span><br><span class="line"><span class="keyword">class</span> <span class="title class_">LongerThan</span> </span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">	<span class="built_in">LongerThan</span>(<span class="type">size_t</span> sz): <span class="built_in">sz</span>(sz) &#123; &#125; </span><br><span class="line">	<span class="function"><span class="type">bool</span> <span class="title">operator</span><span class="params">()</span><span class="params">(<span class="type">const</span> string &amp;s)</span> </span>&#123; <span class="keyword">return</span> s.<span class="built_in">size</span>() &gt; sz; &#125;</span><br><span class="line"><span class="keyword">private</span>:</span><br><span class="line">	<span class="type">size_t</span> sz;</span><br><span class="line">&#125;;</span><br><span class="line"> </span><br><span class="line"><span class="keyword">class</span> <span class="title class_">PrintStr</span> </span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">	<span class="function"><span class="type">void</span> <span class="title">operator</span><span class="params">()</span><span class="params">(<span class="type">const</span> string &amp;s)</span> </span>&#123; cout &lt;&lt; s &lt;&lt; <span class="string">&quot; &quot;</span>; &#125;</span><br><span class="line">&#125;;</span><br><span class="line"> </span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">elimDups</span><span class="params">(vector&lt;string&gt; &amp;words)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="built_in">sort</span>(words.<span class="built_in">begin</span>(), words.<span class="built_in">end</span>());</span><br><span class="line">	<span class="keyword">auto</span> end_unique = <span class="built_in">unique</span>(words.<span class="built_in">begin</span>(), words.<span class="built_in">end</span>());</span><br><span class="line">	words.<span class="built_in">erase</span>(end_unique, words.<span class="built_in">end</span>());</span><br><span class="line">&#125;</span><br><span class="line"> </span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">biggies</span><span class="params">(vector&lt;string&gt; &amp;words, vector&lt;string&gt;::size_type sz)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="built_in">elimDups</span>(words);</span><br><span class="line">	IsShorter is;</span><br><span class="line">	<span class="built_in">stable_sort</span>(words.<span class="built_in">begin</span>(), words.<span class="built_in">end</span>(), is);</span><br><span class="line">	</span><br><span class="line">	<span class="function">LongerThan <span class="title">longer</span><span class="params">(sz)</span></span>;</span><br><span class="line">	<span class="keyword">auto</span> wc = <span class="built_in">find_if</span>(words.<span class="built_in">begin</span>(), words.<span class="built_in">end</span>(), longer);</span><br><span class="line">	<span class="keyword">auto</span> cnt = words.<span class="built_in">end</span>() - wc;</span><br><span class="line">	</span><br><span class="line">	PrintStr ps;</span><br><span class="line">	for_each(wc, words.<span class="built_in">end</span>(), ps);</span><br><span class="line">			 </span><br><span class="line">	cout &lt;&lt; endl;</span><br><span class="line">&#125;</span><br><span class="line"> </span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">char</span> *argv[])</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="function">ifstream <span class="title">in</span><span class="params">(argv[<span class="number">1</span>])</span></span>;</span><br><span class="line">	string line, word;</span><br><span class="line">	vector&lt;string&gt; words;</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">while</span> (<span class="built_in">getline</span>(in, line)) </span><br><span class="line">	&#123;</span><br><span class="line">		<span class="function">istringstream <span class="title">text</span><span class="params">(line)</span></span>;</span><br><span class="line">		<span class="keyword">while</span> (text &gt;&gt; word)</span><br><span class="line">			words.<span class="built_in">push_back</span>(word);</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="built_in">biggies</span>(words, <span class="number">4</span>);</span><br><span class="line">	</span><br><span class="line">	<span class="built_in">system</span>(<span class="string">&quot;pause&quot;</span>);</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="Ex14-41"><a href="#Ex14-41" class="headerlink" title="Ex14.41"></a>Ex14.41</h2><p>lambda通过匿名的函数对象实现，可看作函数对象的简化方式；当函数只用一次，可以用lambda实现，需要多次使用则用函数对象。</p>
<h1 id="Exercises-Section-14-8-2"><a href="#Exercises-Section-14-8-2" class="headerlink" title="Exercises Section 14.8.2"></a>Exercises Section 14.8.2</h1><h2 id="Ex14-42"><a href="#Ex14-42" class="headerlink" title="Ex14.42"></a>Ex14.42</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;vector&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;algorithm&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;functional&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="keyword">using</span> std::placeholders::_1;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	vector&lt;<span class="type">int</span>&gt; v1 = &#123;<span class="number">1024</span>, <span class="number">2025</span>, <span class="number">510</span>, <span class="number">4568</span>&#125;;</span><br><span class="line">	vector&lt;<span class="type">int</span>&gt; v2 = &#123;<span class="number">2</span>, <span class="number">4</span>, <span class="number">6</span>, <span class="number">8</span>&#125;;</span><br><span class="line">	vector&lt;string&gt; v3 = &#123;<span class="string">&quot;pooh&quot;</span>, <span class="string">&quot;pool&quot;</span>, <span class="string">&quot;fool&quot;</span>&#125;;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">auto</span> wc = <span class="built_in">count_if</span>(v1.<span class="built_in">begin</span>(), v1.<span class="built_in">end</span>(), <span class="built_in">bind</span>(<span class="built_in">greater</span>&lt;<span class="type">int</span>&gt;(), _1, <span class="number">1024</span>));</span><br><span class="line">	<span class="keyword">auto</span> iter = <span class="built_in">find_if</span>(v3.<span class="built_in">begin</span>(), v3.<span class="built_in">end</span>(), <span class="built_in">bind</span>(<span class="built_in">not_equal_to</span>&lt;string&gt;(), _1, <span class="string">&quot;pooh&quot;</span>));</span><br><span class="line">	<span class="built_in">transform</span>(v2.<span class="built_in">begin</span>(), v2.<span class="built_in">end</span>(), v2.<span class="built_in">begin</span>(), <span class="built_in">bind</span>(<span class="built_in">multiplies</span>&lt;<span class="type">int</span>&gt;(), _1, <span class="number">2</span>));</span><br><span class="line">	cout &lt;&lt; wc &lt;&lt; <span class="string">&quot; &quot;</span> &lt;&lt; *iter &lt;&lt; endl;;</span><br><span class="line">	<span class="keyword">for</span> (<span class="keyword">auto</span> &amp;i : v2)</span><br><span class="line">		cout &lt;&lt; i &lt;&lt; <span class="string">&quot; &quot;</span>;</span><br><span class="line">	cout &lt;&lt; endl;</span><br><span class="line"></span><br><span class="line">	<span class="built_in">system</span>(<span class="string">&quot;pause&quot;</span>);</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="Ex14-43"><a href="#Ex14-43" class="headerlink" title="Ex14.43"></a>Ex14.43</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;vector&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;algorithm&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;functional&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="keyword">using</span> std::placeholders::_1;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">bool</span> <span class="title">DivByAll</span><span class="params">(<span class="type">const</span> vector&lt;<span class="type">int</span>&gt; &amp;vec, <span class="type">int</span> div)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="keyword">return</span> !<span class="built_in">count_if</span>(vec.<span class="built_in">begin</span>(), vec.<span class="built_in">end</span>(), <span class="built_in">bind</span>(<span class="built_in">modulus</span>&lt;<span class="type">int</span>&gt;(), _1, div));</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	vector&lt;<span class="type">int</span>&gt; v1 = &#123;<span class="number">1024</span>, <span class="number">2025</span>, <span class="number">510</span>, <span class="number">4568</span>&#125;;</span><br><span class="line">	vector&lt;<span class="type">int</span>&gt; v2 = &#123;<span class="number">2</span>, <span class="number">4</span>, <span class="number">6</span>, <span class="number">8</span>&#125;;</span><br><span class="line"></span><br><span class="line">	cout &lt;&lt; <span class="built_in">DivByAll</span>(v1, <span class="number">3</span>) &lt;&lt; <span class="string">&quot; &quot;</span> &lt;&lt; <span class="built_in">DivByAll</span>(v2, <span class="number">2</span>) &lt;&lt; endl;</span><br><span class="line">	<span class="built_in">system</span>(<span class="string">&quot;pause&quot;</span>);</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h1 id="Exercises-Section-14-8-3"><a href="#Exercises-Section-14-8-3" class="headerlink" title="Exercises Section 14.8.3"></a>Exercises Section 14.8.3</h1><h2 id="Ex14-44"><a href="#Ex14-44" class="headerlink" title="Ex14.44"></a>Ex14.44</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;map&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;functional&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;string&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;algorithm&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">add</span><span class="params">(<span class="type">int</span> i, <span class="type">int</span> j)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="keyword">return</span> i + j;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">divide</span></span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">	<span class="function"><span class="type">int</span> <span class="title">operator</span><span class="params">()</span><span class="params">(<span class="type">int</span> denominator, <span class="type">int</span> divisor)</span></span></span><br><span class="line"><span class="function">	</span>&#123;</span><br><span class="line">		<span class="keyword">return</span> denominator / divisor;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="keyword">auto</span> mod = [] (<span class="type">int</span> i, <span class="type">int</span> j) &#123; <span class="keyword">return</span> i % j; &#125;;</span><br><span class="line"></span><br><span class="line">map&lt;string, function&lt;<span class="type">int</span>(<span class="type">int</span>, <span class="type">int</span>)&gt;&gt; binops = </span><br><span class="line">&#123;</span><br><span class="line">	&#123;<span class="string">&quot;+&quot;</span>, add&#125;,</span><br><span class="line">	&#123;<span class="string">&quot;-&quot;</span>, std::<span class="built_in">minus</span>&lt;<span class="type">int</span>&gt;()&#125;,</span><br><span class="line">	&#123;<span class="string">&quot;/&quot;</span>, <span class="built_in">divide</span>()&#125;,</span><br><span class="line">	&#123;<span class="string">&quot;*&quot;</span>, [](<span class="type">int</span> i, <span class="type">int</span> j) &#123; <span class="keyword">return</span> i * j; &#125;&#125;,</span><br><span class="line">	&#123;<span class="string">&quot;%&quot;</span>, mod&#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="type">int</span> lhs, rhs;</span><br><span class="line">	string op;</span><br><span class="line">	cin &gt;&gt; lhs &gt;&gt; op &gt;&gt; rhs;</span><br><span class="line">	cout &lt;&lt; <span class="string">&quot;result: &quot;</span> &lt;&lt; binops[op](lhs, rhs) &lt;&lt; endl;</span><br><span class="line">	<span class="built_in">system</span>(<span class="string">&quot;pause&quot;</span>);</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h1 id="Exercises-Section-14-9-1"><a href="#Exercises-Section-14-9-1" class="headerlink" title="Exercises Section 14.9.1"></a>Exercises Section 14.9.1</h1><h2 id="Ex14-45"><a href="#Ex14-45" class="headerlink" title="Ex14.45"></a>Ex14.45</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">Sales_data</span></span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">	<span class="function"><span class="keyword">operator</span> <span class="title">string</span><span class="params">()</span> <span class="type">const</span> </span>&#123; <span class="keyword">return</span> bookNo; &#125;</span><br><span class="line">	<span class="function"><span class="keyword">operator</span> <span class="title">ddouble</span><span class="params">()</span> <span class="type">const</span> </span>&#123; <span class="keyword">return</span> revenue; &#125;</span><br><span class="line">	<span class="comment">// 其他公有成员、私有成员、友元不变</span></span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<h2 id="Ex14-46"><a href="#Ex14-46" class="headerlink" title="Ex14.46"></a>Ex14.46</h2><p>不应定义以上两种类型转换，只有bookNo, units_sold 和 revenue 三者组合才有意义；应该定义成 explicit 的，防止进行默认的转换。</p>
<h2 id="Ex14-47"><a href="#Ex14-47" class="headerlink" title="Ex14.47"></a>Ex14.47</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">struct</span> <span class="title class_">Integral</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="function"><span class="keyword">operator</span> <span class="type">const</span> <span class="title">int</span><span class="params">()</span></span>;		<span class="comment">// 将 Integral 转换为 const int</span></span><br><span class="line">	<span class="function"><span class="keyword">operator</span> <span class="title">int</span><span class="params">()</span> <span class="type">const</span></span>;		<span class="comment">// 将 const Integral 转换为 int</span></span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<h2 id="Ex14-48"><a href="#Ex14-48" class="headerlink" title="Ex14.48"></a>Ex14.48</h2><p>定义的是 Book 类，可以定义一个 bool 类型的转换运算符，用来检查书籍是否有效；应该声明为 explicit，防止自动转换引发错误。</p>
<h2 id="Ex14-49"><a href="#Ex14-49" class="headerlink" title="Ex14.49"></a>Ex14.49</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">Book</span></span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">	<span class="function"><span class="keyword">operator</span> <span class="title">bool</span><span class="params">()</span></span>;</span><br><span class="line">	<span class="comment">// 其他公有成员、私有成员、友元不变</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h1 id="Exercises-Section-14-9-2"><a href="#Exercises-Section-14-9-2" class="headerlink" title="Exercises Section 14.9.2"></a>Exercises Section 14.9.2</h1><h2 id="Ex14-50"><a href="#Ex14-50" class="headerlink" title="Ex14.50"></a>Ex14.50</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// ex1: LongDouble::operator double() or LongDouble::operator float() 产生二义性</span></span><br><span class="line"><span class="comment">// ex2: LongDouble::operator float()</span></span><br><span class="line"><span class="keyword">struct</span> <span class="title class_">LongDouble</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="built_in">LongDouble</span>(<span class="type">double</span> = <span class="number">0.0</span>);</span><br><span class="line">	<span class="function"><span class="keyword">operator</span> <span class="title">double</span><span class="params">()</span></span>;</span><br><span class="line">	<span class="function"><span class="keyword">operator</span> <span class="title">float</span><span class="params">()</span></span>;</span><br><span class="line">&#125;;</span><br><span class="line">LongDouble ldObj;</span><br><span class="line"><span class="type">int</span> ex1 = ldObj;</span><br><span class="line"><span class="type">float</span> ex2 = ldObj;</span><br></pre></td></tr></table></figure>

<h2 id="Ex14-51"><a href="#Ex14-51" class="headerlink" title="Ex14.51"></a>Ex14.51</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* void calc(int)，将 double 转换为 int，是标准类型转换</span></span><br><span class="line"><span class="comment"> * 而void calc(LongDouble) 转换为用户自定义类型</span></span><br><span class="line"><span class="comment"> * 标准类型优先</span></span><br><span class="line"><span class="comment"> * 故而 calc(dval) 会调用 void calc(int)</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">calc</span><span class="params">(<span class="type">int</span>)</span></span>;		</span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">calc</span><span class="params">(LongDouble)</span></span>;</span><br><span class="line"><span class="type">double</span> dval;</span><br><span class="line"><span class="built_in">calc</span>(dval);	</span><br></pre></td></tr></table></figure>

<h1 id="Exercises-Section-14-9-3"><a href="#Exercises-Section-14-9-3" class="headerlink" title="Exercises Section 14.9.3"></a>Exercises Section 14.9.3</h1><h2 id="Ex14-52"><a href="#Ex14-52" class="headerlink" title="Ex14.52"></a>Ex14.52</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">SmallInt</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="keyword">friend</span> SmallInt <span class="keyword">operator</span>+(<span class="type">const</span> SmallInt &amp;, <span class="type">const</span> SmallInt &amp;);</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">	<span class="built_in">SmallInt</span>(<span class="type">int</span> = <span class="number">0</span>);</span><br><span class="line">	<span class="function"><span class="keyword">operator</span> <span class="title">int</span><span class="params">()</span> <span class="type">const</span> </span>&#123; <span class="keyword">return</span> val; &#125;</span><br><span class="line"><span class="keyword">private</span>:</span><br><span class="line">	std::<span class="type">size_t</span> val;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="keyword">struct</span> <span class="title class_">LongDouble</span></span><br><span class="line">&#123;</span><br><span class="line">	LongDouble <span class="keyword">operator</span>+(<span class="type">const</span> SmallInt &amp;);</span><br><span class="line">	<span class="built_in">LongDouble</span>(<span class="type">double</span> = <span class="number">0.0</span>);</span><br><span class="line">	<span class="function"><span class="keyword">operator</span> <span class="title">double</span><span class="params">()</span></span>;</span><br><span class="line">	<span class="function"><span class="keyword">operator</span> <span class="title">float</span><span class="params">()</span></span>;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">LongDouble <span class="keyword">operator</span>+(LongDouble &amp;, <span class="type">double</span>);</span><br><span class="line"></span><br><span class="line">SmallInt si;</span><br><span class="line">LongDouble ld;</span><br><span class="line">ld = si + ld;	</span><br><span class="line">ld = ld + si;</span><br></pre></td></tr></table></figure>

<p>对于 <font color=red>ld &#x3D; si + ld</font><br>LongDouble 类型无法转换为 SmallInt 类型，故而 SmallInt 的 friend operator+ 不适用；<br>SmallInt 类型无法转换为 LongDouble 类型，LongDouble 的成员 operator+ 和非成员 operator+ 都不适用；<br>SmallInt 可转换为 int，LongDouble 可转换为 double 或 float，成员operator+ 和非成员 operator+都适用，产生二义性。</p>
<p>对于<font color=red>ld &#x3D; ld + si</font><br>LongDouble 无法转换为 SmallInt，SmallInt 的 friend operator+ 不适用，LongDouble成员 operator+(const SmallInt &amp;) 精确匹配；<br>SmallInt 无法转换为 LongDouble，故 LongDouble 的成员 operator+ 和非成员 operator+ 都不适用；<br>故而使用LongDouble成员 operator+(const SmallInt &amp;)</p>
<h2 id="Ex14-53"><a href="#Ex14-53" class="headerlink" title="Ex14.53"></a>Ex14.53</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">SmallInt</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="keyword">friend</span> SmallInt <span class="keyword">operator</span>+(<span class="type">const</span> SmallInt &amp;, <span class="type">const</span> SmallInt &amp;);</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">	<span class="built_in">SmallInt</span>(<span class="type">int</span> = <span class="number">0</span>);</span><br><span class="line">	<span class="function"><span class="keyword">operator</span> <span class="title">int</span><span class="params">()</span> <span class="type">const</span> </span>&#123; <span class="keyword">return</span> val; &#125;</span><br><span class="line"><span class="keyword">private</span>:</span><br><span class="line">	std::<span class="type">size_t</span> val;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">SmallInt sl;</span><br><span class="line"><span class="type">double</span> d = sl + <span class="number">3.14</span>;</span><br></pre></td></tr></table></figure>

<p>友元 operator+ 适用，可以将 3.14 转换为 int 类型，然后再从 int 类型转换为 SmallInt 类型；<br>也可以将 sl 转换为  int 类型，故而产生二义性。</p>
<p>更改如下：</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">SmallInt sl;</span><br><span class="line"><span class="type">double</span> d = sl + <span class="built_in">SmallInt</span>(<span class="number">3.14</span>);</span><br></pre></td></tr></table></figure>

</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="https://astralcon.gitee.io/blog">astralcon</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://astralcon.gitee.io/blog/2022/05/14/Chapter-14-Overloaded-Operations-And-Conversions/">https://astralcon.gitee.io/blog/2022/05/14/Chapter-14-Overloaded-Operations-And-Conversions/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://astralcon.gitee.io/blog" target="_blank">A君的秘密基地</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/blog/tags/C-Primer/">C++ Primer</a></div><div class="post_share"><div class="social-share" data-image="" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/social-share.js/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/gh/overtrue/share.js@master/dist/js/social-share.min.js" defer></script></div></div><div class="post-reward"><div class="reward-button"><i class="fas fa-qrcode"></i> 打赏</div><div class="reward-main"><ul class="reward-all"><li class="reward-item"><a href="/blog/img/wechat.jpg" target="_blank"><img class="post-qr-code-img" src="/blog/img/wechat.jpg" alt="微信"/></a><div class="post-qr-code-desc">微信</div></li><li class="reward-item"><a href="/blog/img/alipay.jpg" target="_blank"><img class="post-qr-code-img" src="/blog/img/alipay.jpg" alt="支付宝"/></a><div class="post-qr-code-desc">支付宝</div></li></ul></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/blog/2022/05/14/Chapter-15-Object-Oriented-Programming/"><img class="prev-cover" src="" onerror="onerror=null;src='/blog/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">Chapter 15: Object-Oriented Programming</div></div></a></div><div class="next-post pull-right"><a href="/blog/2022/05/14/Chapter-13-Copy-Control/"><img class="next-cover" src="" onerror="onerror=null;src='/blog/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">Chapter 13: Copy Control</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>相关推荐</span></div><div class="relatedPosts-list"><div><a href="/blog/2022/05/14/Chapter-1-Getting-Started/" title="Chapter 1: Getting Started"><img class="cover" src="" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-05-14</div><div class="title">Chapter 1: Getting Started</div></div></a></div><div><a href="/blog/2022/05/14/Chapter-10-Generic-Algorithms/" title="Chapter 10: Generic Algorithms"><img class="cover" src="" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-05-14</div><div class="title">Chapter 10: Generic Algorithms</div></div></a></div><div><a href="/blog/2022/05/14/Chapter-11-Associative-Containers/" title="Chapter 11: Associative Containers"><img class="cover" src="" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-05-14</div><div class="title">Chapter 11: Associative Containers</div></div></a></div><div><a href="/blog/2022/05/14/Chapter-12-Dynamic-Memory/" title="Chapter 12: Dynamic Memory"><img class="cover" src="" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-05-14</div><div class="title">Chapter 12: Dynamic Memory</div></div></a></div><div><a href="/blog/2022/05/14/Chapter-13-Copy-Control/" title="Chapter 13: Copy Control"><img class="cover" src="" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-05-14</div><div class="title">Chapter 13: Copy Control</div></div></a></div><div><a href="/blog/2022/05/14/Chapter-15-Object-Oriented-Programming/" title="Chapter 15: Object-Oriented Programming"><img class="cover" src="" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-05-14</div><div class="title">Chapter 15: Object-Oriented Programming</div></div></a></div></div></div><hr/><div id="post-comment"><div class="comment-head"><div class="comment-headline"><i class="fas fa-comments fa-fw"></i><span> 评论</span></div><div id="comment-switch"><span class="first-comment">Valine</span><span class="switch-btn"></span><span class="second-comment">Disqus</span></div></div><div class="comment-wrap"><div><div class="vcomment" id="vcomment"></div></div><div><div id="disqus_thread"></div></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="/blog/img/avatar.jpg" onerror="this.onerror=null;this.src='/blog/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">astralcon</div><div class="author-info__description">slow is fast.</div></div><div class="card-info-data site-data is-center"><a href="/blog/archives/"><div class="headline">文章</div><div class="length-num">27</div></a><a href="/blog/tags/"><div class="headline">标签</div><div class="length-num">4</div></a><a href="/blog/categories/"><div class="headline">分类</div><div class="length-num">3</div></a></div><div class="card-info-social-icons is-center"><a class="social-icon" href="https://github.com/astralcon" target="_blank" title="Github"><i class="fab fa-github"></i></a><a class="social-icon" href="mailto:a842181471@qq.com.com" target="_blank" title="Email"><i class="fas fa-envelope"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>公告</span></div><div class="announcement_content">朝九晚五，健康生活！</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex14-1"><span class="toc-number">1.</span> <span class="toc-text">Ex14.1</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex14-2"><span class="toc-number">2.</span> <span class="toc-text">Ex14.2</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex14-3"><span class="toc-number">3.</span> <span class="toc-text">Ex14.3</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex14-4"><span class="toc-number">4.</span> <span class="toc-text">Ex14.4</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex14-5"><span class="toc-number">5.</span> <span class="toc-text">Ex14.5</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#Exercises-Section-14-2-1"><span class="toc-number"></span> <span class="toc-text">Exercises Section 14.2.1</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex14-6"><span class="toc-number">1.</span> <span class="toc-text">Ex14.6</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex14-7"><span class="toc-number">2.</span> <span class="toc-text">Ex14.7</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex14-8"><span class="toc-number">3.</span> <span class="toc-text">Ex14.8</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#Exercises-Section-14-2-2"><span class="toc-number"></span> <span class="toc-text">Exercises Section 14.2.2</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex14-9"><span class="toc-number">1.</span> <span class="toc-text">Ex14.9</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex14-10"><span class="toc-number">2.</span> <span class="toc-text">Ex14.10</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex14-11"><span class="toc-number">3.</span> <span class="toc-text">Ex14.11</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex14-12"><span class="toc-number">4.</span> <span class="toc-text">Ex14.12</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#Exercises-Section-14-3"><span class="toc-number"></span> <span class="toc-text">Exercises Section 14.3</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex14-13"><span class="toc-number">1.</span> <span class="toc-text">Ex14.13</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex14-14"><span class="toc-number">2.</span> <span class="toc-text">Ex14.14</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex14-15"><span class="toc-number">3.</span> <span class="toc-text">Ex14.15</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#Exercises-Section-14-3-1"><span class="toc-number"></span> <span class="toc-text">Exercises Section 14.3.1</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex14-16"><span class="toc-number">1.</span> <span class="toc-text">Ex14.16</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex14-17"><span class="toc-number">2.</span> <span class="toc-text">Ex14.17</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#Exercises-Section-14-3-2"><span class="toc-number"></span> <span class="toc-text">Exercises Section 14.3.2</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex14-18"><span class="toc-number">1.</span> <span class="toc-text">Ex14.18</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex14-19"><span class="toc-number">2.</span> <span class="toc-text">Ex14.19</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#Exercises-Section-14-4"><span class="toc-number"></span> <span class="toc-text">Exercises Section 14.4</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex14-20"><span class="toc-number">1.</span> <span class="toc-text">Ex14.20</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex14-21"><span class="toc-number">2.</span> <span class="toc-text">Ex14.21</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex14-22"><span class="toc-number">3.</span> <span class="toc-text">Ex14.22</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex14-23"><span class="toc-number">4.</span> <span class="toc-text">Ex14.23</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex14-24"><span class="toc-number">5.</span> <span class="toc-text">Ex14.24</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex14-25"><span class="toc-number">6.</span> <span class="toc-text">Ex14.25</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#Exercises-Section-14-5"><span class="toc-number"></span> <span class="toc-text">Exercises Section 14.5</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex14-26"><span class="toc-number">1.</span> <span class="toc-text">Ex14.26</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#Exercises-Section-14-6"><span class="toc-number"></span> <span class="toc-text">Exercises Section 14.6</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex14-27"><span class="toc-number">1.</span> <span class="toc-text">Ex14.27</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex14-28"><span class="toc-number">2.</span> <span class="toc-text">Ex14.28</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex14-29"><span class="toc-number">3.</span> <span class="toc-text">Ex14.29</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#Exercises-Section-14-7"><span class="toc-number"></span> <span class="toc-text">Exercises Section 14.7</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex14-30"><span class="toc-number">1.</span> <span class="toc-text">Ex14.30</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex14-31"><span class="toc-number">2.</span> <span class="toc-text">Ex14.31</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex14-32"><span class="toc-number">3.</span> <span class="toc-text">Ex14.32</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#Exercises-Section-14-8"><span class="toc-number"></span> <span class="toc-text">Exercises Section 14.8</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex14-33"><span class="toc-number">1.</span> <span class="toc-text">Ex14.33</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex14-34"><span class="toc-number">2.</span> <span class="toc-text">Ex14.34</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex14-35"><span class="toc-number">3.</span> <span class="toc-text">Ex14.35</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex14-36"><span class="toc-number">4.</span> <span class="toc-text">Ex14.36</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex14-37"><span class="toc-number">5.</span> <span class="toc-text">Ex14.37</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#Exercises-Section-14-8-1"><span class="toc-number"></span> <span class="toc-text">Exercises Section 14.8.1</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex14-38"><span class="toc-number">1.</span> <span class="toc-text">Ex14.38</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex14-39"><span class="toc-number">2.</span> <span class="toc-text">Ex14.39</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex14-40"><span class="toc-number">3.</span> <span class="toc-text">Ex14.40</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex14-41"><span class="toc-number">4.</span> <span class="toc-text">Ex14.41</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#Exercises-Section-14-8-2"><span class="toc-number"></span> <span class="toc-text">Exercises Section 14.8.2</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex14-42"><span class="toc-number">1.</span> <span class="toc-text">Ex14.42</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex14-43"><span class="toc-number">2.</span> <span class="toc-text">Ex14.43</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#Exercises-Section-14-8-3"><span class="toc-number"></span> <span class="toc-text">Exercises Section 14.8.3</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex14-44"><span class="toc-number">1.</span> <span class="toc-text">Ex14.44</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#Exercises-Section-14-9-1"><span class="toc-number"></span> <span class="toc-text">Exercises Section 14.9.1</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex14-45"><span class="toc-number">1.</span> <span class="toc-text">Ex14.45</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex14-46"><span class="toc-number">2.</span> <span class="toc-text">Ex14.46</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex14-47"><span class="toc-number">3.</span> <span class="toc-text">Ex14.47</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex14-48"><span class="toc-number">4.</span> <span class="toc-text">Ex14.48</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex14-49"><span class="toc-number">5.</span> <span class="toc-text">Ex14.49</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#Exercises-Section-14-9-2"><span class="toc-number"></span> <span class="toc-text">Exercises Section 14.9.2</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex14-50"><span class="toc-number">1.</span> <span class="toc-text">Ex14.50</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex14-51"><span class="toc-number">2.</span> <span class="toc-text">Ex14.51</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#Exercises-Section-14-9-3"><span class="toc-number"></span> <span class="toc-text">Exercises Section 14.9.3</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex14-52"><span class="toc-number">1.</span> <span class="toc-text">Ex14.52</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex14-53"><span class="toc-number">2.</span> <span class="toc-text">Ex14.53</span></a></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/blog/2022/05/17/%E3%80%8A%E7%AE%97%E6%B3%95%E8%AE%BE%E8%AE%A1%E6%89%8B%E5%86%8C%E3%80%8B%E9%9D%A2%E8%AF%95%E9%A2%98%EF%BC%9AChapter-1/" title="《算法设计手册》面试题：Chapter 1">《算法设计手册》面试题：Chapter 1</a><time datetime="2022-05-17T07:35:05.000Z" title="发表于 2022-05-17 15:35:05">2022-05-17</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/blog/2022/05/14/Wireshark-Lab-DHCP-v7-0/" title="Wireshark Lab: DHCP v7.0">Wireshark Lab: DHCP v7.0</a><time datetime="2022-05-14T07:19:53.000Z" title="发表于 2022-05-14 15:19:53">2022-05-14</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/blog/2022/05/14/Wireshark-Lab-Ethernet-and-ARP-v7-0/" title="Wireshark Lab: Ethernet and ARP v7.0">Wireshark Lab: Ethernet and ARP v7.0</a><time datetime="2022-05-14T07:18:10.000Z" title="发表于 2022-05-14 15:18:10">2022-05-14</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/blog/2022/05/14/Wireshark-Lab-ICMP-v7-0/" title="Wireshark Lab: ICMP v7.0">Wireshark Lab: ICMP v7.0</a><time datetime="2022-05-14T07:16:41.000Z" title="发表于 2022-05-14 15:16:41">2022-05-14</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/blog/2022/05/14/Wireshark-Lab-NAT-v7-0/" title="Wireshark Lab: NAT v7.0">Wireshark Lab: NAT v7.0</a><time datetime="2022-05-14T07:15:13.000Z" title="发表于 2022-05-14 15:15:13">2022-05-14</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2022 By astralcon</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><a id="to_comment" href="#post-comment" title="直达评论"><i class="fas fa-comments"></i></a><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div id="algolia-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">搜索</span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="search-wrap"><div id="algolia-search-input"></div><hr/><div id="algolia-search-results"><div id="algolia-hits"></div><div id="algolia-pagination"></div><div id="algolia-info"><div class="algolia-stats"></div><div class="algolia-poweredBy"></div></div></div></div></div><div id="search-mask"></div></div><div><script src="/blog/js/utils.js"></script><script src="/blog/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.umd.js"></script><script src="https://cdn.jsdelivr.net/npm/algoliasearch@4/dist/algoliasearch-lite.umd.js"></script><script src="https://cdn.jsdelivr.net/npm/instantsearch.js@4/dist/instantsearch.production.min.js"></script><script src="/blog/js/search/algolia.js"></script><script>var preloader = {
  endLoading: () => {
    document.body.style.overflow = 'auto';
    document.getElementById('loading-box').classList.add("loaded")
  },
  initLoading: () => {
    document.body.style.overflow = '';
    document.getElementById('loading-box').classList.remove("loaded")

  }
}
window.addEventListener('load',preloader.endLoading())</script><div class="js-pjax"><script>function loadValine () {
  function initValine () {
    const valine = new Valine(Object.assign({
      el: '#vcomment',
      appId: 'Uh3CYGE2t1Eaz4KoTkyVQv1N-gzGzoHsz',
      appKey: '1Gdg4rGlmT4Dcfv7oNiCH1q3',
      avatar: 'monsterid',
      serverURLs: '',
      emojiMaps: "",
      path: window.location.pathname,
      visitor: false
    }, null))
  }

  if (typeof Valine === 'function') initValine() 
  else getScript('https://cdn.jsdelivr.net/npm/valine/dist/Valine.min.js').then(initValine)
}

if ('Valine' === 'Valine' || !false) {
  if (false) btf.loadComment(document.getElementById('vcomment'),loadValine)
  else setTimeout(loadValine, 0)
} else {
  function loadOtherComment () {
    loadValine()
  }
}</script><script>function loadDisqus () {
  var disqus_config = function () {
    this.page.url = 'https://astralcon.gitee.io/blog/2022/05/14/Chapter-14-Overloaded-Operations-And-Conversions/'
    this.page.identifier = '2022/05/14/Chapter-14-Overloaded-Operations-And-Conversions/'
    this.page.title = 'Chapter 14: Overloaded Operations And Conversions'
  };

  window.disqusReset = () => {
    DISQUS.reset({
      reload: true,
      config: disqus_config
    })
  }

  if (window.DISQUS) disqusReset()
  else {
    (function() { 
      var d = document, s = d.createElement('script');
      s.src = 'https://.disqus.com/embed.js';
      s.setAttribute('data-timestamp', +new Date());
      (d.head || d.body).appendChild(s);
    })();
  }
}

if ('Valine' === 'Disqus' || !false) {
  if (false) btf.loadComment(document.getElementById('disqus_thread'), loadDisqus)
  else loadDisqus()
} else {
  function loadOtherComment () {
    loadDisqus()
  }
}
</script></div><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div><script src="/blog/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"log":false,"pluginJsPath":"lib/","pluginModelPath":"assets/","pluginRootPath":"live2dw/","tagMode":false});</script></body></html>